package com.ruoyi.front.ap.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.front.ap.pojo.vo.*;
import com.ruoyi.front.ap.service.ILsdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author：HuaQP
 * @Date：2023/10/7
 */
@Anonymous
@Api(value = "LsdController", tags = {"农产品大屏_接口"})
@RestController
@RequestMapping(value = "/lsd")
public class LsdController {

    @Resource
    private ILsdService lsdService;

    @ApiOperation("01_获取产品分类汇总")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年份", dataType = "Long", paramType = "query", required = true),
            @ApiImplicitParam(name = "categoryId", value = "产品分类id,可空", dataType = "Long", paramType = "query")
    })
    @GetMapping("/listByCategory")
    public AjaxResult summaryByCategory(Long year, @RequestParam(value = "categoryId", required = false) Long categoryId) {
        List<CategoryPlantVo> listPlant = lsdService.listPlantCategory(year, categoryId);
        List<CategorySaleVo> listSale = lsdService.listSaleCategory(year, categoryId);
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("listPlant", listPlant);
        ajaxResult.put("listSale", listSale);
        return ajaxResult;
    }

    @ApiOperation("02_获取产品认证情况")
    @GetMapping("/listCertified")
    public AjaxResult listCertified() {
        List<CertifiedVo> listCertified = lsdService.listCertified();
        return AjaxResult.success(listCertified);
    }

    @ApiOperation("03_获取产品类近5年销售数量情况")
    @ApiImplicitParam(name = "year", value = "年份", dataType = "Long", paramType = "query", required = true)
    @GetMapping("/listBySale")
    public AjaxResult listBySale(Long year) {
        List<CategorySaleVo> list = lsdService.listSaleByNearly5Years(year);
        return AjaxResult.success(list);
    }

    @ApiOperation("04_获取各村种植产值情况")
    @ApiImplicitParam(name = "year", value = "年份", dataType = "Long", paramType = "query", required = true)
    @GetMapping("/listByVillage")
    public AjaxResult listByVillage(Long year) {
        List<VillagePlantVo> listPlant = lsdService.listPlantVillage(year);
        List<VillageSaleVo> listSale = lsdService.listSaleVillage(year);
        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("listPlant", listPlant);
        ajaxResult.put("listSale", listSale);
        return ajaxResult;
    }

    @ApiOperation("05_获取地图数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年份", dataType = "Long", paramType = "query", required = true),
            @ApiImplicitParam(name = "categoryId", value = "产品分类id，可选", dataType = "Long", paramType = "query")
    })
    @GetMapping("/listMapData")
    public AjaxResult listMapData(Long year, @RequestParam(value = "categoryId", required = false) Long categoryId) {
        List<MapVillageVo> listVillage = lsdService.listVillageVo(year, categoryId);
        List<MapPlantVo> listPlant = lsdService.listPlantVo(year, categoryId);
        double totalAcreage = listPlant.stream().mapToDouble(item -> item.getAcreage().doubleValue()).sum();
        List<CategorySaleVo> listSale = lsdService.listSaleCategory(year, categoryId);
        double totalCount = listSale.stream().mapToDouble(item -> item.getCount().doubleValue()).sum();
        double totalAmount = listSale.stream().mapToDouble(item -> item.getAmount().doubleValue()).sum();

        AjaxResult ajaxResult = AjaxResult.success();
        ajaxResult.put("listVillage", listVillage);
        ajaxResult.put("listPlant", listPlant);
        ajaxResult.put("listSale", listSale);
        ajaxResult.put("totalAcreage", totalAcreage);
        ajaxResult.put("totalCount", totalCount);
        ajaxResult.put("totalAmount", totalAmount);

        return ajaxResult;
    }

    @ApiOperation("06_获取子产品占比情况")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年份", dataType = "Long", paramType = "query", required = true),
            @ApiImplicitParam(name = "categoryId", value = "产品分类id", dataType = "Long", paramType = "query", required = true),
    })
    @GetMapping("/listByProduct")
    public AjaxResult listByProduct(Long year,Long categoryId) {
        List<ProductSaleVo> list = lsdService.listSaleByProduct(year,categoryId);
        return AjaxResult.success(list);
    }

    @ApiOperation("07_获取销售方式情况")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年份", dataType = "Long", paramType = "query", required = true),
            @ApiImplicitParam(name = "categoryId", value = "产品分类id", dataType = "Long", paramType = "query", required = true),
    })
    @GetMapping("/listByWay")
    public AjaxResult listByWay(Long year,Long categoryId) {
        List<SaleWayVo> list = lsdService.listByWay(year,categoryId);
        return AjaxResult.success(list);
    }



}
